﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WW.NActiveRecord.Internal.Utils
{
    /// <summary>
    /// 惰性初始化器
    /// </summary>
    class InertInitObject<T> {
        private readonly Func<T> loadValue;
        private T value;

        public InertInitObject(Func<T> loadValue) {
            this.loadValue = loadValue;
        }

        public void InitByHand(T value) {
            this.value = value;
        }

        public T Get() {
            if (Equals(value, default(T))) {
                value = loadValue();
            }
            return value;
        }
    }
}